package com.atguigu.ucenter_member.service.impl;
import com.atguigu.commonutils.exceptionhandler.GuliException;
import com.atguigu.commonutils.jwt.JwtUtils;
import com.atguigu.commonutils.jwt.MD5;
import com.atguigu.ucenter_member.dao.UcenterMemberDao;
import com.atguigu.ucenter_member.entity.UcenterMemberEntity;
import com.atguigu.ucenter_member.entity.vo.RegisterVo;
import com.atguigu.ucenter_member.service.UcenterMemberService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

@Service("ucenterMemberService")
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberDao, UcenterMemberEntity> implements UcenterMemberService {

    @Resource
    private UcenterMemberDao ucenterMemberDao;

    @Resource(name= "stringRedisTemplate")
    private StringRedisTemplate stringRedisTemplate;
    /**
     *   需求分析
     *      * 通过邮箱和密码进行登录
     *      * wrapper：邮箱密码都不能为空
     *      *          邮箱和密码要和数据库匹配
     *                 是否被禁用
     *      *          密码用MD5加密方式
     *      *          根据id和email返回32位小写加密方式的token字符串
     * @param ucenterMemberEntity
     * @return token
     */
    @Override
    public String login(UcenterMemberEntity ucenterMemberEntity) {
        //邮箱密码为空return exception
        if(StringUtils.isEmpty(ucenterMemberEntity.getEmail()) || StringUtils.isEmpty(ucenterMemberEntity.getPassword())){
          throw  new GuliException(20001,"邮箱或者密码不能为空");
        }
        //根据邮箱数据库查询一组数据
        UcenterMemberEntity ucenterMemberEntitySql=ucenterMemberDao.selectOne(new QueryWrapper<UcenterMemberEntity>()
                .eq("email",ucenterMemberEntity.getEmail()));
        if(!ucenterMemberEntity.getEmail().equals(ucenterMemberEntitySql.getEmail())){
            throw  new GuliException(20001,"邮箱错误");
        }
        if(!MD5.encrypt(ucenterMemberEntity.getPassword()).equals(ucenterMemberEntitySql.getPassword())){
            throw  new GuliException(20001,"密码错误");
        }
        if(ucenterMemberEntitySql.getIsDisabled()>0){
            throw  new GuliException(20001,"账户被禁止使用，重新注册");
        }
        String token=JwtUtils.getJwtToken(ucenterMemberEntitySql.getId(),ucenterMemberEntitySql.getEmail());
        return token;
    }

    /**
     *   需求分析
     *      * 判断redis验证码和输入的验证码是否相同
     *      * 判断邮箱是否已被注册
     * @param registerVo
     * @return token
     */
    @Override
    public void insert(RegisterVo registerVo) {
        String code= registerVo.getCode();
        String nickname=registerVo.getNickname();
        String password=registerVo.getPassword();
        String email=registerVo.getEmail();
       if(StringUtils.isEmpty(code)
               || StringUtils.isEmpty(nickname)
               || StringUtils.isEmpty(password)
               || StringUtils.isEmpty(email)){
           throw  new GuliException(20001,"昵称，密码，邮箱和验证码不能为空，注册失败");
       }
        String redisCode=stringRedisTemplate.opsForValue().get(email);
       if(!code.equals(redisCode)){
           throw new GuliException(20001,"验证码不对，注册失败");
       }
       Integer count= ucenterMemberDao.selectCount(new QueryWrapper<UcenterMemberEntity>().eq("email",email));
       if(count>0){
           throw new GuliException(20001,"邮箱已注册，请登录");
       }
       UcenterMemberEntity ucenterMemberEntity=new UcenterMemberEntity();
       ucenterMemberEntity.setNickname(nickname);
       ucenterMemberEntity.setPassword(MD5.encrypt(password));
        ucenterMemberEntity.setEmail(email);
       ucenterMemberDao.insert(ucenterMemberEntity);
    }

    @Override
    public UcenterMemberEntity getInfoByToken(String memberId) {
        UcenterMemberEntity ucenterMemberEntity= ucenterMemberDao.selectById(memberId);
        return ucenterMemberEntity;
    }

    @Override
    public UcenterMemberEntity selectOpenId(String openid) {
        UcenterMemberEntity user=ucenterMemberDao.selectOne(new QueryWrapper<UcenterMemberEntity>().eq("openid",openid));
        return user;
    }
}